{extend name="platform/base" /}
{block name="resources"/}
<style>
.error{
	display:none;
	color:red;
}
</style>
{/block}
{block name="main"}
<section class="panel panel-default">
	<header class="panel-heading font-bold"> 添加版块 </header>
	<div class="panel-body">
		<form class="form-horizontal">
			<div class="form-group">
				<label class="col-sm-2 control-label">版块名称</label>
				<div class="col-md-2">
					<input type="text" class="form-control" id="block_name" value="">
				</div>
				<p class="error">请输入版块名称</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">简称</label>
				<div class="col-md-2">
					<input type="text" class="form-control" id="block_short_name" value="">
				</div>
				<p class="error">请输入版块简称</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">颜色风格</label>
				<div class="col-md-2">
					<input type="color" class="form-control" id="block_color" value="" style="width:74px;height:34px;">
				</div>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">排序</label>
				<div class="col-md-1">
					<input type="text" class="form-control" id="sort" value="">
				</div>
				<p class="error">请输入序号</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">是否显示</label>
				<div class="col-sm-2">
					<label class="switch"> <input id="is_display"
						type="checkbox" checked> <span></span>
					</label>
				</div>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">推荐单图广告位</label>
				<div class="col-md-3">
					<input type="text" class="form-control" id="recommend_ad_image_name" placeholder="请输入推荐单图广告位名称" value="">
				</div>
				<div class="col-md-3">
					<select name="account" class="form-control m-b"
						id="recommend_ad_image">
						<option value="0">请选择推荐单图广告位</option>
						{volist name="recommend_ad_image_list" id="va1"}
							<option value="{$va1['ap_id']}">{$va1['ap_name']}</option>
						{/volist}
					</select>
				</div>
				<p class="error">推荐单图广告位及名称不能为空</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">推荐多图广告位</label>
				<div class="col-md-3">
					<input type="text" class="form-control"
						id="recommend_ad_images_name" placeholder="请输入推荐多图广告位名称" value="">
				</div>
				<div class="col-md-3">
					<select name="account" class="form-control m-b"
						id="recommend_ad_images">
						<option value="0">请选择推荐多图广告位</option>
						{volist name="recommend_ad_images_list" id="va2"}
							<option value="{$va2['ap_id']}">{$va2['ap_name']}</option>
						{/volist}
					</select>
				</div>
				<p class="error">推荐多图广告位及名称不能为空</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">推荐幻灯片广告位</label>
				<div class="col-md-3">
					<input type="text" class="form-control"
						id="recommend_ad_slide_name" placeholder="请输入推荐幻灯片广告位名称" value="">
				</div>
				<div class="col-md-3">
					<select name="account" class="form-control m-b"
						id="recommend_ad_slide">
						<option value="0">请选择推荐幻灯片广告位</option>
						{volist name="recommend_ad_slide_list" id="va3"}
							<option value="{$va3['ap_id']}">{$va3['ap_name']}</option>
						{/volist}
					</select>
				</div>
				<p class="error">推荐幻灯片广告位及名称不能为空</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">推荐商品1</label>
				<div class="col-md-3">
					<input type="text" class="form-control" id="recommend_goods_category_name_1" placeholder="请输入推荐分类商品别名" value="">
				</div>
				<select name="account" class="form-control m-b category_first" id="goods_category_first_1" style="width:145px;height:34px;display:inline-block;">
					<option value="0">请选择一级分类</option>
					{volist name="goods_category_list" id="vo"}
						{if condition="$vo.level eq 1"}
							<option value="{$vo['category_id']}">{$vo['category_name']}</option>
						{/if}
					{/volist}
				</select>
				<select name="account" class="form-control m-b hides category_second" id="goods_category_second_1" style="width:145px;height:34px;">
					<option value="0">请选择二级分类</option>
					{volist name="goods_category_list" id="vo"}
						{if condition="$vo.level eq 2"}
							<option value="{$vo['category_id']}" pid="{$vo['pid']}" style="display: none;">{$vo['category_name']}</option>
						{/if}
					{/volist}
				</select>
				<select name="account" class="form-control m-b hides category_three" id="goods_category_three_1"  style="width:145px;height:34px;">
					<option value="0">请选择三级分类</option>
					{volist name="goods_category_list" id="vo"}
						{if condition="$vo.level eq 3"}
							<option value="{$vo['category_id']}" pid="{$vo['pid']}">{$vo['category_name']}</option>
						{/if}
					{/volist}
				</select>
				<p class="error"><label class="col-sm-2 control-label" style="margin-right: 12px;"></label>推荐分类商品别名及分类选择不可为空</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">推荐商品2</label>
				<div class="col-md-3">
					<input type="text" class="form-control"
						id="recommend_goods_category_name_2" placeholder="请输入推荐分类商品别名"
						value="">
				</div>
				<select name="account" class="form-control m-b category_first" id="goods_category_first_2" style="width:145px;height:34px;display:inline-block;">
					<option value="0">请选择一级分类</option>
					{volist name="goods_category_list" id="vo"}
						{if condition="$vo.level eq 1"}
							<option value="{$vo['category_id']}">{$vo['category_name']}</option>
						{/if}
					{/volist}
				</select>
				<select name="account" class="form-control m-b hides category_second" id="goods_category_second_2" style="width:145px;height:34px;">
					<option value="0">请选择二级分类</option>
					{volist name="goods_category_list" id="vo"}
						{if condition="$vo.level eq 2"}
							<option value="{$vo['category_id']}" pid="{$vo['pid']}">{$vo['category_name']}</option>
						{/if}
					{/volist}
				</select>
				<select name="account" class="form-control m-b hides category_three" id="goods_category_three_2"  style="width:145px;height:34px;">
					<option value="0">请选择三级分类</option>
					{volist name="goods_category_list" id="vo"}
						{if condition="$vo.level eq 3"}
							<option value="{$vo['category_id']}" pid="{$vo['pid']}">{$vo['category_name']}</option>
						{/if}
					{/volist}
				</select>
				<p class="error"><label class="col-sm-2 control-label" style="margin-right: 12px;"></label>推荐分类商品别名及分类选择不可为空</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">推荐商品3</label>
				<div class="col-md-3">
					<input type="text" class="form-control"
						id="recommend_goods_category_name_3" placeholder="请输入推荐分类商品别名"
						value="">
				</div>
				<select name="account" class="form-control m-b category_first" id="goods_category_first_3" style="width:145px;height:34px;display:inline-block;">
					<option value="0">请选择一级分类</option>
					{volist name="goods_category_list" id="vo"}
						{if condition="$vo.level eq 1"}
							<option value="{$vo['category_id']}">{$vo['category_name']}</option>
						{/if}
					{/volist}
				</select>
				<select name="account" class="form-control m-b hides category_second" id="goods_category_second_3" style="width:145px;height:34px;">
					<option value="0">请选择二级分类</option>
					{volist name="goods_category_list" id="vo"}
						{if condition="$vo.level eq 2"}
							<option value="{$vo['category_id']}" pid="{$vo['pid']}">{$vo['category_name']}</option>
						{/if}
					{/volist}
				</select>
				<select name="account" class="form-control m-b hides category_three" id="goods_category_three_3"  style="width:145px;height:34px;">
					<option value="0">请选择三级分类</option>
					{volist name="goods_category_list" id="vo"}
						{if condition="$vo.level eq 3"}
							<option value="{$vo['category_id']}" pid="{$vo['pid']}">{$vo['category_name']}</option>
						{/if}
					{/volist}
				</select>
				<p class="error"><label class="col-sm-2 control-label" style="margin-right: 12px;"></label>推荐分类商品别名及分类选择不可为空</p>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">推荐品牌</label>
				<div class="col-sm-10">
				{volist name="goods_brand_list" id="vb"}
					<label class="checkbox-inline"> <input type="checkbox"
						name="recommend_brands" value="{$vb['brand_id']}"> {$vb['brand_name']}
					</label>
				{/volist}
				</div>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>
			<div class="form-group">
				<label class="col-sm-2 control-label">推荐商品分类</label>
				<div class="col-sm-10">
				{volist name="goods_category_list" id="vc"}
					<label class="checkbox-inline"> <input type="checkbox"
						name="recommend_categorys" value="{$vc['category_id']}"> {$vc['category_name']}
					</label>
				{/volist}
				</div>
			</div>
			<div class="line line-dashed line-lg pull-in"></div>



			<div class="form-group">
				<div class="col-sm-4 col-sm-offset-2">
					<a href="javascript:void(0);" onclick="addBlockAjax()"
						class="btn btn-primary">保存</a>
				</div>
			</div>
		</form>
	</div>
</section>
<script>	
$(function(){
	$('.category_first').change(function(){
		var pid = $(this).val();
		//alert(JSON.stringify($(this).parent().html()));
		var obj_category_second = $(this).parent().children('.category_second');
		//obj_category_second.show();
		obj_category_second.children('option:gt(0)').hide().parent().hide();
		var category_second_option_obj = obj_category_second.children('option[pid="'+pid+'"]')
		category_second_option_obj.show().parent().show();
		obj_category_second.val(0);
		obj_category_second.trigger('change');
	})
	$('.category_second').change(function(){
		var pid = $(this).val();
		var obj_category_three = $(this).parent().children('.category_three')
		obj_category_three.val(0);
		obj_category_three.children('option:gt(0)').hide().parent().hide();
		obj_category_three.children('option[pid="'+pid+'"]').show().parent().show();
	})
})


	/**
	*添加广告位
	*/
	function addBlockAjax() {
		var block_name = $("#block_name").val();
		var block_short_name = $("#block_short_name").val();
		var block_color = $("#block_color").val();
		var sort = $("#sort").val();
		if($("#is_display").prop("checked")){
			var is_display = 1;
		}else{
			var is_display = 0;
		}
		var recommend_ad_image_name = $("#recommend_ad_image_name").val();
		var recommend_ad_image = $("#recommend_ad_image").val();
		var recommend_ad_images_name = $("#recommend_ad_images_name").val();
		var recommend_ad_images = $("#recommend_ad_images").val();
		var recommend_ad_slide_name = $("#recommend_ad_slide_name").val();
		var recommend_ad_slide = $("#recommend_ad_slide").val();
		var recommend_brands = '';
		$("input[type='checkbox'][name='recommend_brands']:checked").each(function(){ 
			recommend_brands += $(this).val()+","; 
		});
		recommend_brands = recommend_brands.substring(0,recommend_brands.length-1);
		var recommend_categorys = '';
		$("input[type='checkbox'][name='recommend_categorys']:checked").each(function(){ 
			recommend_categorys += $(this).val()+","; 
		});
		recommend_categorys = recommend_categorys.substring(0,recommend_categorys.length-1);
		var recommend_goods_category_name_1 = $("#recommend_goods_category_name_1").val();
// 		var recommend_goods_category_1 = $("#recommend_goods_category_1").val();
		var recommend_goods_category_name_2 = $("#recommend_goods_category_name_2").val();
// 		var recommend_goods_category_2 = $("#recommend_goods_category_2").val();
		var recommend_goods_category_name_3 = $("#recommend_goods_category_name_3").val();
// 		var recommend_goods_category_3 = $("#recommend_goods_category_3").val();
		if($("#goods_category_three_1").val() > 0){
			var recommend_goods_category_1 = $("#goods_category_three_1").val();
		}else if($("#goods_category_second_1").val() > 0){
			var recommend_goods_category_1 = $("#goods_category_second_1").val();
		}else{
			var recommend_goods_category_1 = $("#goods_category_first_1").val();
		}
		if($("#goods_category_three_2").val() > 0){
			var recommend_goods_category_2 = $("#goods_category_three_2").val();
		}else if($("#goods_category_second_2").val() > 0){
			var recommend_goods_category_2 = $("#goods_category_second_2").val();
		}else{
			var recommend_goods_category_2 = $("#goods_category_first_2").val();
		}
		if($("#goods_category_three_3").val() > 0){
			var recommend_goods_category_3 = $("#goods_category_three_3").val();
		}else if($("#goods_category_second_3").val() > 0){
			var recommend_goods_category_3 = $("#goods_category_second_3").val();
		}else{
			var recommend_goods_category_3 = $("#goods_category_first_3").val();
		}
		
		if(verify(block_name, recommend_ad_image_name, recommend_ad_image, recommend_ad_images_name, recommend_ad_images, recommend_ad_slide_name, recommend_ad_slide,
				recommend_goods_category_name_1, recommend_goods_category_1, recommend_goods_category_name_2, recommend_goods_category_2, recommend_goods_category_name_3, recommend_goods_category_3)){
// 		alert(block_name);
// 		alert(block_short_name);
// 		alert(block_color);
// 		alert(sort);
// 		alert(is_display);
// 		alert(recommend_ad_image_name);
// 		alert(recommend_ad_image);
// 		alert(recommend_ad_images_name);
// 		alert(recommend_ad_images);
// 		alert(recommend_ad_slide_name);
// 		alert(recommend_ad_slide);
// 		alert(recommend_brands);
// 		alert(recommend_categorys);
// 		alert(recommend_goods_category_name_1);
// 		alert(recommend_goods_category_1);
// 		alert(recommend_goods_category_name_2);
// 		alert(recommend_goods_category_2);
// 		alert(recommend_goods_category_name_3);
// 		alert(recommend_goods_category_3);
//  		verify(ad_image, link_url);
		$.ajax({
			type : "post",
			url : "{:__URL('PLATFORM_MAIN/Config/addBlock')}",
			data : {
				'block_name' : block_name,
				'block_short_name' : block_short_name,
				'block_color' : block_color,
				'sort' : sort,
				'is_display' : is_display,
				'recommend_ad_image_name' : recommend_ad_image_name,
				'recommend_ad_image' : recommend_ad_image,
				'recommend_ad_images_name' : recommend_ad_images_name,
				'recommend_ad_images' : recommend_ad_images,
				'recommend_ad_slide_name' : recommend_ad_slide_name,
				'recommend_ad_slide' : recommend_ad_slide,
				'recommend_brands' : recommend_brands,
				'recommend_categorys' : recommend_categorys,
				'recommend_goods_category_name_1' : recommend_goods_category_name_1,
				'recommend_goods_category_1' : recommend_goods_category_1,
				'recommend_goods_category_name_2' : recommend_goods_category_name_2,
				'recommend_goods_category_2' : recommend_goods_category_2,
				'recommend_goods_category_name_3' : recommend_goods_category_name_3,
				'recommend_goods_category_3' : recommend_goods_category_3
			},
			async : true,
			success : function(data) {
				if (data["code"] > 0) {
					showMessage('success', "添加成功","{:__URL('PLATFORM_MAIN/Config/blockList')}");
				}else{
					showMessage('error', "添加失败");
				}
			}
		});
		
		}
	}
	/**
	*模块输入信息验证
	*/
	function verify(
			block_name, 
			recommend_ad_image_name, recommend_ad_image, 
			recommend_ad_images_name, recommend_ad_images, 
			recommend_ad_slide_name, recommend_ad_slide,
			recommend_goods_category_name_1, recommend_goods_category_1, 
			recommend_goods_category_name_2, recommend_goods_category_2, 
			recommend_goods_category_name_3, recommend_goods_category_3
			){
		if(block_name == ''){
			$("#block_name").parent().next().show();
			$("#block_name").focus();
			return false;
		}else{
			$(".error").hide();
		}
		
		if(recommend_ad_image_name == ''){
			$("#recommend_ad_image_name").parent().next().next().show();
			return false;
		}else if(recommend_ad_image == 0){
			$("#recommend_ad_image").parent().next().show();
			return false;
		}else{
			$(".error").hide();
		}
		if(recommend_ad_images_name == ''){
			$("#recommend_ad_images_name").parent().next().next().show();
			return false;
		}else if(recommend_ad_images == 0){
			$("#recommend_ad_images").parent().next().show();
			return false;
		}else{
			$(".error").hide();
		}
		if(recommend_ad_slide_name == ''){
			$("#recommend_ad_slide_name").parent().next().next().show();
			return false;
		}else if(recommend_ad_slide == 0){
			$("#recommend_ad_slide").parent().next().show();
			return false;
		}else{
			$(".error").hide();
		}
		if(recommend_goods_category_name_1 == '' || recommend_goods_category_1 == 0){
			$("#recommend_goods_category_name_1").parent().siblings('.error').show();
			return false;
		}else{
			$(".error").hide();
		}
		if(recommend_goods_category_name_2 == '' || recommend_goods_category_2 == 0){
			$("#recommend_goods_category_name_2").parent().siblings('.error').show();
			return false;
		}else{
			$(".error").hide();
		}
		if(recommend_goods_category_name_3 == '' || recommend_goods_category_3 == 0){
			$("#recommend_goods_category_name_3").parent().siblings('.error').show();
			return false;
		}else{
			$(".error").hide();
		}
		return true;
	}
</script>
{/block}
